//
//  PhotoBorwserCell.m
//
//  Created by mac on 2023/3/1.
//

#import "PhotoBorwserCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>

@interface PhotoBorwserCell()<UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIActivityIndicatorView *indicatorView;

@end

@implementation PhotoBorwserCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self setupView];
    return self;
}

- (void)setupView {
    __weak typeof(self) weakSelf = self;
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    _scrollView.delegate = self;
    [_scrollView setMinimumZoomScale:1.0];
    [_scrollView setMaximumZoomScale:3.0];

    _imageView = [[UIImageView alloc] init];
    [_scrollView addSubview:_imageView];
    _imageView.userInteractionEnabled = YES;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        make.width.equalTo(weakSelf.mas_width);
        make.height.equalTo(weakSelf.mas_height);
    }];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 1;
    [_imageView addGestureRecognizer:tapGesture];
    
    if (@available(iOS 13.0, *)){
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
    }else{
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    }
    _indicatorView.center = [UIApplication sharedApplication].delegate.window.center;
    _indicatorView.bounds = CGRectMake(0, 0, 80, 80);
    [self addSubview:_indicatorView];
    _indicatorView.hidden = YES;
}

- (void)handleTapGesture:(UITapGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        if(self.optionBlock){
            self.optionBlock();
        }
    }
}

- (void)setUrl:(NSString *)url {
    
    _url = url;
    
    [self sl_setImageWithURL:[NSURL URLWithString:_url]];
    
}

- (void)sl_setImageWithURL:(NSURL *)URL{

    if (URL){
        _indicatorView.hidden = NO;
        [_indicatorView startAnimating];
        __weak typeof(self) weakSelf = self;
        [_imageView sd_setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"define_image"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [weakSelf.indicatorView stopAnimating];
            weakSelf.indicatorView.hidden = YES;
        }];
    }else{
        [_imageView setImage:[UIImage imageNamed:@"define_image"]];
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    return _imageView;
}

@end
